package lxgorm

import (
	"gitee.com/lxgow/lxconv"
	credis "gitee.com/lxgow/lxmysql/gorm/common/redis"
	"gorm.io/gorm"
	"strings"
)

// model
// 创建时间:下午12:18
// 创建人:lixu

// GormSettings 配置参数
type GormSettings struct {
	Debug      bool
	Conns      []MDbConn
	GormConfig gorm.Config
	Cache      MCache
}

// GormConfig sql链接
type GormConfig struct {
	SkipDefaultTransaction bool
}

// MDbConn sql链接
type MDbConn struct {
	Name string
	Conn string
}

// MCache 缓存
type MCache struct {
	Type      string // redis,process
	CacheTime int
	Redis     credis.MRedisConfig
}

// DataParams 请求的参数
type dataParams struct {
	where      map[string]interface{} // where条件
	orderBy    string                 // 排序
	tableName  string                 // 表名字
	dbIndex    string                 // 选择的数据库
	autoCreate bool                   // 是否自动创建
	debug      bool                   // debug
	field      string                 // 字段
	limit      int                    // limit
	offset     int                    // offset
	group      string                 // group
	cache      struct {
		on       bool
		typeName string
		ex       int
		name     string
	}
}

func Db(ds ...string) *dataParams {
	var i = "default"
	if len(ds) > 0 && ds[0] != "" {
		i = ds[0]
	}
	b := &dataParams{
		dbIndex: i,
		limit:   -100,
		offset:  -100,
		debug:   defaultSetting.Debug,
	}

	b.cache.ex = -100

	return b
}

func TableExist(err error) bool {
	if err == nil {
		return true
	}
	if strings.Contains(err.Error(), "doesn't exist") {
		return false
	}
	return true
}

// Where where条件
func (b *dataParams) Where(where map[string]interface{}) *dataParams {
	b.where = where
	return b
}

// SelectDb 选择数据库
func SelectDb(dbIndex string) *gorm.DB {
	return GLOBALDBS[dbIndex]
}

// TableName 选择表名
func (b *dataParams) TableName(tableName string) *dataParams {
	b.tableName = tableName
	return b
}

// Field 字段名
func (b *dataParams) Field(field string) *dataParams {
	b.field = field
	return b
}

// Order 排序
func (b *dataParams) Order(order string) *dataParams {
	b.orderBy = order
	return b
}

// Limit Limit
func (b *dataParams) Limit(limit int) *dataParams {
	b.limit = limit
	return b
}

// Offset Offset
func (b *dataParams) Offset(offset int) *dataParams {
	b.offset = offset
	return b
}

// Group 分组
func (b *dataParams) Group(group string) *dataParams {
	b.group = group
	return b
}

// AutoCreate 分组
func (b *dataParams) AutoCreate(autoCreate bool) *dataParams {
	b.autoCreate = autoCreate
	return b
}

// Debug 分组
func (b *dataParams) Debug() *dataParams {
	b.debug = true
	return b
}

// Cache 缓存
func (b *dataParams) Cache(name string, exts ...string) *dataParams {
	b.cache.on = true
	b.cache.name = name
	b.cache.typeName = defaultSetting.Cache.Type
	b.cache.ex = defaultSetting.Cache.CacheTime
	if len(exts) > 0 {
		b.cache.ex = lxconv.ToInt(exts[0])
	}
	if len(exts) > 1 {
		b.cache.typeName = exts[1]
	}
	return b
}

// UseCache 缓存
func (b *dataParams) UseCache(use bool) *dataParams {
	b.cache.on = use
	b.cache.name = lxconv.EncryMD5(lxconv.JsonEncode(b.where))
	b.cache.typeName = defaultSetting.Cache.Type
	return b
}
